package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Shop;
import cn.wolfcode.luowowo.article.query.ShopQuery;
import cn.wolfcode.luowowo.article.service.IShopService;
import cn.wolfcode.luowowo.comment.domain.PropComment;
import cn.wolfcode.luowowo.comment.query.PropCommentQuery;
import cn.wolfcode.luowowo.comment.service.IPropCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

//积分商城
@Controller
@RequestMapping("/itemshop")
public class ShopController {

    @Reference
    private IShopService shopService;

    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IPropCommentService propCommentService;

    @RequestMapping("/list")
    public String list(Model model,Long userId){

        List<Shop> list = shopService.selectAll();
        model.addAttribute("list",list);

        if(userId != null){
            //拿积分
            UserInfo userInfo = userInfoService.getScoreById(userId);
            model.addAttribute("userInfo",userInfo);
        }

        return "itemshop/list";
    }


    @RequestMapping("/query")
    public String query( @ModelAttribute("qo") ShopQuery qo,Model model,Long userId){

        //查全部
        if(qo.getQueryType() == 0){
            PageInfo pageInfo = shopService.queryAll(qo);
            model.addAttribute("pageInfo",pageInfo);
            String str = UUID.randomUUID().toString().substring(0, 5);
            model.addAttribute("shop",str);
        }

        //查当前用已购买的
        if(qo.getQueryType() == 1){
            // 0表示查全部 1:表示查当前用户的道具
            PageInfo pageInfo = shopService.queryByType(userId,qo);
            model.addAttribute("pageInfo",pageInfo);

            Shop shop = null;
            model.addAttribute("shop",shop);
        }


        return "itemshop/itemTempl";
    }

    //商品购买
    @RequestMapping("/buy")
    @ResponseBody
    public Object buy(Long id,Long userId){ //商品id,用户id

        try {

            //拿用户的积分
            UserInfo userInfo = userInfoService.getScoreById(userId);

            if(userInfo == null){
                throw new RuntimeException("请先登录");
            }

            //---------------------------------------------------

            Long score = userInfo.getScore();

            String shopId = userInfo.getShopId();
            if(shopId != null){
                String[] split = shopId.split(",");
                for (int i = 0; i < split.length; i++) {
                    if(split[i].equals(String.valueOf(id))){
                        throw new RuntimeException("您已经拥有该道具了,请勿重复购买");
                    }
                }
            }

            //拿商品售价
            Shop shop = shopService.selectByPrimaryKey(id);
            Long price = shop.getPrice();

            if(shop.getRest() <= 0){
                throw new RuntimeException("商品已经售空了!");
            }

            if(score < price){
                throw new RuntimeException("您的积分不够!");
            }

            //进行购买c
            //减少用户积分
            userInfoService.updateScore(userInfo.getId(),price); //传入用户id和该减少的分数
            //减少商品库存
            shopService.updateRest(shop.getId());
            //修改用户已购买的商品
            userInfoService.updateBuy(userInfo.getId(),shop.getId());

            return AjaxResult.success();
        }catch (Exception e){

            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());

        }

    }

    //评论
    @RequestMapping("/comment")
    public String comment(@UserParam UserInfo userInfo, String content, Model model, PropCommentQuery qo){

        if(-1 != qo.getFlag()){

            PropComment pct = new PropComment();
            pct.setUserId(userInfo.getId());
            pct.setUserName(userInfo.getNickname());
            pct.setUserImg(userInfo.getHeadImgUrl());
            pct.setCreateTime(new Date());
            pct.setContent(content);
            propCommentService.save(pct);

        }

        Page page = propCommentService.findAll(qo);
        model.addAttribute("page",page);

        return "itemshop/commentTempl";
    }
}
